package com.kye.express.business.deposit_recharge.offline;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.databinding.ItemRechargeOfflineImageBinding;

/**
 * 预存运费-充值（线下）ViewHolder
 * <p>
 * Created by SkyYu on 2018/1/18
 */
class RechargeOfflineImageItemViewBinder extends me.drakeet.multitype.ItemViewBinder<RechargeOfflineImagesModel, RechargeOfflineImageItemViewBinder.ViewHolder> {

    private Context mContext;
    private RechargeOfflineActivity mActivity;

    RechargeOfflineImageItemViewBinder(Context context) {
        mContext = context;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        return new ViewHolder(inflater.inflate(R.layout.item_recharge_offline_image, parent, false));
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull RechargeOfflineImagesModel item) {
        holder.bindData(item);
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private RechargeOfflineImagesModel data;
        private ItemRechargeOfflineImageBinding dataBinding;

        public ViewHolder(View itemView) {
            super(itemView);
            dataBinding = DataBindingUtil.bind(itemView);
            mActivity = (RechargeOfflineActivity) mContext;
        }

        public void bindData(RechargeOfflineImagesModel item) {
            data = item;
            if (data != null) {
                dataBinding.setData(data);
                RxView.clicks(dataBinding.imageviewOfflineAdd)
                        .compose(new DebounceObservableTransformer<>())
                        .subscribe((Object) ->
                                mActivity.showBottomSheet()
                        );
                RxView.clicks(dataBinding.imageviewOfflineDel)
                        .compose(new DebounceObservableTransformer<>())
                        .subscribe((Object) -> {
                                    getAdapter().getItems().remove(item);
                                    mActivity.deletePlaceholderImage();
                                    mActivity.addPlaceholderImage();
                                    getAdapter().notifyDataSetChanged();
                                }
                        );
            }
        }
    }
}